var express = require('express');
var router = express.Router();
//引入数据库模块
const connection = require('./conn')
//连接数据库
connection.connect(()=>{
  console.log('数据库连接成功')
})
//获取商品列表
router.post('/findProduct', function(req,res){
  const sql = `select * from product`
  connection.query(sql,(err,data)=>{
    if(err){
      throw err
    }else{
      res.send(data)
    }
  })
})
//新增用户
router.post('/addUser',function(req,res){
  let {userName,user,pass,age,gender} = req.body
  const sql = `INSERT INTO user (name,userName,passWord,age,gender) values ('${user}','${userName}','${pass}','${age}','${gender}')`
  connection.query(sql,(err,data)=>{
    if(err){
      throw err
    }else{
      res.send(data)
    }
  })
})
/* GET home page. */
router.post('/login', function(req, res) {
  let {userName,password} = req.body
  const sql=`select * from user where userName = '${userName}' and passWord='${password}'`
  connection.query(sql,(err,data)=>{
    if(err){
      throw err
    }else{
      res.send(data)
    }
  })
  console.log(req.body)
});
//查询用户数据
router.post('/findUser',function(req,res){
  const sql = `select * from user`
  connection.query(sql,(err,data)=>{
    console.log(data)
    if(err){
      throw err
    }else{
      res.send(data)
    }
  })
})
router.post('/deleteUser',function(req,res){
  let {id} = req.body
  console.log(id)
  let sql = `delete from user where id='${id}'`
  connection.query(sql,(err,data)=>{
    console.log(err,data)
    if(err){
      throw err
    }else{
      res.send(data)
    }
  })
})

module.exports = router;
